package com.jymf.common;

import com.jymf.model.Resource;
import com.jymf.model.TreeVo;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author zyj
 * @create 2017-06-02
 **/

public class CommonUtils {

    public static Map<String, Object> getLimit(int pageSize, int pageNumber){
        Map<String, Object> limitMap = new HashMap<String,Object>();
        int lastNum = pageSize;
        int firstNum = (pageNumber-1)*pageSize;
        limitMap.put("lastNum", lastNum);
        limitMap.put("firstNum", firstNum);

        return  limitMap;
    }

    /**
     * 判断List是否为空。 list==null或长度为0返回true,其它则返回false.
     *
     * @param list
     * @return
     */
    public static boolean isBlank(List<? extends Object> list) {
        if (list == null || list.size() == 0)
            return true;
        return false;
    }

    /**
     * 判断string类型是否为空(空为true)
     * @param value
     * @return: boolean
     * @author zyj
     */
    public static boolean isEmpty(String value){
        boolean b = true;
        if(value!=null&&!(value.trim()).equals("")){
            b = false;
        }

        return b;
    }

    /**
     * 对象转换
     * @param resourceList
     * @return
     */
    public static List<TreeVo> coverTree(List<Resource> resourceList, String logName){
        List<TreeVo> listTree = new ArrayList<TreeVo>();
        if(resourceList!=null&&resourceList.size()>0){
            Iterator<Resource> iterator = resourceList.iterator();
            while (iterator.hasNext()){

                Resource next = iterator.next();
                int id = next.getId();
                String code = next.getCode();
                if(!logName.equals("root")&&code.equals("resources")){//只有root用户才有资源权限
                    continue;
                }
                TreeVo treeVo = new TreeVo();
                treeVo.setId(String.valueOf(id));
                treeVo.setName(next.getName());

                treeVo.setParentId(String.valueOf(next.getParentId()));
                treeVo.setOpen(true);
                listTree.add(treeVo);
            }
        }

        return  listTree;
    }

    /**
     * 获取地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
